<template>
  <div>
    <template v-if="listData.length > 0">
      <el-row :gutter="24">
        <el-col class="mb20" v-for="(item,index) in listData" :key="index" :span="8">
          <el-card shadow="never" class="list-card">
            <div slot="header" class="tl-center">
              <span>{{item.internalPlanName}}</span>
              <el-tooltip class="item" effect="dark" content="详情" placement="top">
                <i
                  @click.stop="handleDetail(item)"
                  v-if="$auth('satisfaction:detail')"
                  class="fr curp mr10 fs16 el-icon-document"
                />
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="复制新增" placement="top">
                <i
                  @click.stop="handleEdit(true, item)"
                  v-if="$auth('satisfaction:add')"
                  class="fr curp mr10 fs16 el-icon-folder"
                />
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="删除" placement="top">
                <i
                  @click.stop="handleDelete(item)"
                  v-if="$auth('satisfaction:delete')"
                  class="red mr10 fr curp fs16 el-icon-delete"
                />
              </el-tooltip>
            </div>
            <el-form class="scheme-form" label-width="110px" label-suffix="：">
              <el-form-item label="任务类型">
                <span>{{ item.internalPlanType && computeTitle(item.internalPlanType) }}</span>
              </el-form-item>
              <el-form-item label="考评任务周期">
                <span>{{ judgeRange(item.internalPlanCycle) }}</span>
              </el-form-item>
              <el-form-item label="任务日期">
                <span>{{ item.startDate ? formatTime(item.startDate, 'day') : null }}{{ item.endDate ? `至${formatTime(item.endDate, 'day')}` : null }}</span>
              </el-form-item>
              <el-form-item label="创建人">{{ item.createUser }}</el-form-item>
              <el-form-item label="创建时间">{{ item.createTime ? formatTime(item.createTime) : null }}</el-form-item>
              <el-form-item label="任务状态">
                <span>
                  <el-tag :type="judgeType(item.state)">{{ judgeState(item.state) }}</el-tag>
                </span>
                <el-tooltip
                  v-if="item.state === 2"
                  class="item"
                  effect="dark"
                  content="作废"
                  placement="top"
                >
                  <i
                    @click.stop="handleChangeState(item)"
                    class="ml10 curp red fs16 el-icon-error"
                  />
                </el-tooltip>
                <el-tooltip
                  v-if="item.state === 1"
                  class="item"
                  effect="dark"
                  content="启用"
                  placement="top"
                >
                  <i
                    @click.stop="handleChangeState(item)"
                    class="ml10 curp fs16 el-icon-video-play"
                  />
                </el-tooltip>
              </el-form-item>
              <el-form-item
                label="评价方形式"
              >{{ item.executorMode === 1 ? '指定部门' : ( item.executorMode === 2 ? '指定岗位' : '指定人员' ) }}</el-form-item>
              <el-form-item
                label="评价方"
                class="overflow-item"
              >{{ item.internalPlanExecutorList ? computeExecutor( item.internalPlanExecutorList ) : null }}</el-form-item>
              <el-form-item
                label="被评价方形式"
              >{{ item.targetMode === 1 ? '指定部门' : ( item.targetMode === 2 ? '指定岗位' : '指定人员' ) }}</el-form-item>
              <el-form-item
                label="被评价方"
                class="overflow-item"
              >{{ item.internalPlanTargetList ? computeExecutor( item.internalPlanTargetList ) : null }}</el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-col class="tl-center nomoreWord" v-show="noMore">---- 这是我的底线 ----</el-col>
      </el-row>
    </template>
    <template v-else>
      <empty-comp ref="emptyComp" />
    </template>
  </div>
</template>

<script>
import { openNotification, judgeRange, judgeEvalState, judgeType } from '@/utils/util'
import { planPage, planUpdateParam, planDelete } from '@/api/satisfaction'
export default {
  data() {
    return {
      listData: [],
      total: 1,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      },
      noMore: false
    }
  },
  mounted() {
    this.getPlanList()
    window.addEventListener('scroll', this.getScroll)
  },
  destroyed() {
    window.removeEventListener('scroll', this.getScroll)
  },
  methods: {
    getScroll() {
      if (document.documentElement.scrollTop + document.documentElement.clientHeight >= document.documentElement.scrollHeight) {
        this.loadMore()
      }
    },
    loadMore() {
      if (this.queryParam.pageNo * 10 > this.total) {
        this.noMore = true
      } else {
        this.queryParam.pageNo += 1
        this.getPlanList(this.queryParam)
      }
    },
    computeTitle(num) {
      let str = ''
      switch (num) {
        case 1:
          str = '科间互评'
          break
        case 2:
          str = '科内互评'
          break
        case 3:
          str = '评上级领导'
          break
        case 4:
          str = '自定义'
          break
        default:
          break
      }
      return str
    },
    // 处理数组数据显示
    computeExecutor(arr) {
      let executor = []
      arr.map(v => {
        executor.push(v.name)
      })
      return executor.join('、')
    },
    // 删除
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.internalPlanName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await planDelete(row.internalPlanId, {})
          openNotification('success', '删除成功')
          const index = _this.listData.findIndex(v => v.internalPlanId === row.internalPlanId)
          _this.listData.splice(index, 1)
          // _this.handleGetList()
        }
      })
    },
    getPlanList(param = {}) {
      this.queryParam = Object.assign(this.queryParam, param)
      planPage(this.queryParam).then(res => {
        console.log('planPage', res)
        if (this.queryParam.pageNo === 1) {
          this.listData = []
          this.listData = [...res.data.records]
        } else {
          this.listData = [...this.listData, ...res.data.records]
        }
        this.total = res.data.total
        if (this.queryParam.pageNo * 10 > this.total) {
          this.noMore = true
        }
      })
    },
    judgeType(num) {
      return judgeType(num)
    },
    judgeState(num) {
      return judgeEvalState(num)
    },
    judgeRange(num) {
      return judgeRange(num)
    },
    async handleChangeState(item) {
      const _this = this
      if (item.state === 2) {
        this.$confirm({
          title: '作废操作',
          content: `您确定作废[${item.internalPlanName}]这条记录吗？`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          centered: true,
          async onOk() {
            // 调用删除接口，刷新表格数据
            const res = await planUpdateParam({ internalPlanId: item.internalPlanId, state: 4 })
            if (res.state === '000000') {
              openNotification('success', '作废操作成功')
              const index = _this.listData.findIndex(v => v.internalPlanId === item.internalPlanId)
              _this.$set(_this.listData[index], 'state', 4)
            }
          }
        })
      } else if (item.state === 1) {
        const res = await planUpdateParam({ internalPlanId: item.internalPlanId, state: 2 })
        if (res.state === '000000') {
          openNotification('success', '启用操作成功')
          const index = _this.listData.findIndex(v => v.internalPlanId === item.internalPlanId)
          _this.$set(_this.listData[index], 'state', 2)
          // _this.getPlanList()
        }
      }
    },
    handleDetail(row) {
      this.$router.push({
        name: 'satisfactionSchemeDetail',
        params: { row }
      })
    },
    // handleEvaluate(row) {
    //   this.$router.push({
    //     name: 'satisfactionSchemeDetail',
    //     params: { row }
    //   })
    // },
    handleEdit(bool, row) {
      console.log(bool, row)
      this.$router.push({
        name: 'satisfactionCreateScheme',
        params: { type: 'edit', row }
      })
    }
  }
}
</script>

<style scoped>
.scheme-form >>> .overflow-item {
  white-space: nowrap;
}
.el-form-item {
  margin-bottom: 10px;
}
</style>